print("hello python")

a =0 
b = 1
i = 100
while i >0:
    print(a)
    c = a+b
    a = b
    b = c
    i = i-1

users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}
for user, status in users.copy().items():
    if status == 'inactive':
        del users[user]
print(users)


print(range(10))
print(list(range(10)))

def errorCode(code:int):
    match code:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"
print(errorCode(404))
print(errorCode(418))

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# color = input("Enter a color (red, green, blue): ")  
# match color:
#     case "red":
#         print(Color.RED)
#     case "green":
#         print(Color.GREEN)
#     case "blue":
#         print(Color.BLUE)
#     case _:
#         print("Invalid color")





add =  lambda x: x+1
print(add(10))

size = lambda x,y : x*y
print(size(10,20))

from functools import reduce

numbers = [1, 2, 3, 4, 5]
sum = reduce(lambda x, y: x + y, numbers)
print(sum)

import p0.p2 as p

print(dir(p))


year = 2024
month = 11
day = 5

print(f"Today is {year:04d}-{month:02d}-{day:02d}")

table = {k: str(v) for k, v in vars().items()}
print(table.keys())
print(table.values())

for k,v in table.items():
   
    print(f'{k}:'+'{' + k +'};')


message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()])
print(message)
print(message.format(**table))


for i in range(10):
    print('{0:2d}{1:3d}{2:4d}'.format(i,i*i,i*i*i))

for i in range(10):
    print(f'{i:2d}{i*i:3d}{i*i*i:4d}')

for i in range(10):
    print(repr(i).rjust(2), repr(i*i).rjust(3), repr(i*i*i).rjust(4))

print(hex(100))
print(oct(100))


def findMinAndMax(arr):
    if len(arr) == 0:
        return None,None
    min = arr[0]
    max = arr[0]
    for i in arr:
        if i < min:
            min = i
        if i > max:
            max = i
    return min,max


if findMinAndMax([]) != (None, None):
    print('测试失败!')
elif findMinAndMax([7]) != (7, 7):
    print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):
    print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('测试失败!')
else:
    print('测试成功!')


def isinstance(value,t):
    if type(value) == t:
        return True
    else:
        return False

if isinstance(100,int):
    print('测试成功!')
else:
    print('测试失败!')

if isinstance(100,str) ==False:
    print('测试成功!')
else:
    print('测试失败!')

if isinstance(100.0,float):
    print('测试成功!')
else:
    print('测试失败!')

if isinstance('100',str):
    print('测试成功!')
else:
    print('测试失败!')

if isinstance(None,str) ==False:
    print('测试成功!')
else:
    print('测试失败!')



L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [ s.lower() for s in L1 if isinstance(s,str)]

# 测试:
print(L2)
if L2 == ['hello', 'world', 'apple']:
    print('测试通过!')
else:
    print('测试失败!')


def yanghui(max):
    a = [1]
    n =  1
    while n <max:
        yield a
        # print(f"a:{a} len:{len(a)}")
        temp = (a[i]+a[i+1] for i in range(len(a)-1))
        # print(f"temp->:{temp}")
        b = [1]
        while True:
            try:
                new = next(temp)
                # print(f"new:{new}")
                b.append(new)

                # a.append(new)
            except StopIteration as e:
                # print("e:{0}".format(e.value))
                break

        b.append(1)
        # print(f"b=:{b}")
        a = b
        n = n+1
        # print(f"a2=:{a}")

        # a = [1] + (a[i]+a[i+1] for i in range(len(a)-1)) + [1]
        # n = n+1
    return 'done'

for i in yanghui(10):
    print(i)


# yh = yanghui(10)

# while True:
#     try:
#         print(next(yh))
#     except StopIteration as e:
#         print("Generator return value:"+e.value)
#         break


def normalize(name):
    if len(name)==0:
        return ""
    else:
        return name[0].upper()+name[1:].lower()


# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)


from functools import reduce

def prod(L):
    return reduce(lambda x,y:x*y,L)

if prod([3, 5, 7, 9]) == 945:
    print('测试成功!')
else:
    print('测试失败!')

def sum(L):
    return reduce(lambda x,y:x+y,L)
if sum([3, 5, 7, 9]) == 24:
    print('测试成功!')
else:
    print('测试失败!')



from functools import reduce

def str2float(s):

    f1 = map(getNum,s)
    print(f"f1 = {f1}")
   
    i = 0
    for j in s:
      if j =='.':
          break
      else:
          i = i +1
    print(f". index {i}")   

    # a = s[0:i]
    b = list(map(getNum,s[i+1:]))
    # na =  a
    isF=  False
    if s[0] == "-":
        na = list(map(getNum,s[1:i])) 
        isF = True
    else:
        na = list(map(getNum,s[0:i]))
    total = na +b
    total1 = reduce(lambda x,y :x*10 +y,total) 
    print(f"total:{total}")
    print(f"len b: {len(b)}")
    lenB=  len(b)
    if isF:
        return -1 * total1 / (10 ** lenB)
    else:
        return total1 / (10 ** lenB)


def getNum(s):
     numDict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
     return numDict[s]

def t(x,y):
    return getNum(x) *10 +getNum(y)
    

print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
    print('测试成功!')
else:
    print('测试失败!')


print('str2float(\'-123.456\') =', str2float('-123.456'))
if abs(str2float('-123.456') + 123.456) == 0:
    print('测试成功!')
else:
    print('测试失败!')


CHAR_TO_FLOAT = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, ".": -1}


def str2float1(s):
    nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
    point = 0

    def to_float(f, n):
        nonlocal point
        if n == -1:
            point = 1
            return f
        if point == 0:
            return f * 10 + n
        else:
            point = point * 10
            return f + n / point

    return reduce(to_float, nums, 0)

print('str2float(\'123.456\') =', str2float1('123.456'))